package com.hanter.demo.raddemo;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.content.Intent;
import android.content.res.Resources;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.Button;

import androidx.appcompat.app.AppCompatActivity;

import com.hanter.android.radlib.util.WindowUtils;

public class SplashActivity extends AppCompatActivity {

    private static final String TAG = "SplashActivity";

    private static final WindowManager.LayoutParams matchParent =
            new WindowManager.LayoutParams(WindowManager.LayoutParams.MATCH_PARENT,
                    WindowManager.LayoutParams.MATCH_PARENT);

    private View launchView;
    private Button btnSkip;

    private int counter = 3;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        WindowUtils.setTranslucentStatus(this, true);
        super.onCreate(savedInstanceState);

        this.launchView = createLaunchView();

        ViewGroup contentParent = findViewById(android.R.id.content);
        View contentView = createContentView(contentParent);

        if (launchView == null) {
            setContentView(contentView);
        } else {
            addContentView(this.launchView, matchParent);
            addContentView(contentView, matchParent);
            getWindow().setBackgroundDrawable(new ColorDrawable(Color.WHITE));
        }

        this.launchView.animate().alpha(0).setListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                ((ViewGroup) SplashActivity.this.launchView.getParent()).removeView(SplashActivity.this.launchView);
                SplashActivity.this.launchView = null;
            }
        }).start();


        btnSkip = findViewById(R.id.btnSkip);
        btnSkip.setOnClickListener(v -> {
            getWindow().getDecorView().removeCallbacks(countDownRunnable);
            Intent intent = new Intent(SplashActivity.this, MainActivity.class);
            startActivity(intent);
            finish();
        });

        getWindow().getDecorView().postDelayed(countDownRunnable, 1000);
    }

    @Override
    protected void onDestroy() {
        getWindow().getDecorView().removeCallbacks(countDownRunnable);
        super.onDestroy();
    }

    private final Runnable countDownRunnable = new Runnable() {
        @Override
        public void run() {
            counter--;
            btnSkip.setText(counter + "s");
            if (counter <= 0) {
                Intent intent = new Intent(SplashActivity.this, MainActivity.class);
                startActivity(intent);
                finish();
            } else {
                getWindow().getDecorView().postDelayed(countDownRunnable, 1000);
            }
        }
    };

    private View createContentView(ViewGroup parent) {
        return LayoutInflater.from(this).inflate(R.layout.activity_splash, parent, false);
    }

    private View createLaunchView() {
        if (!this.showSplashScreenUntilFirstFrame()) {
            return null;
        } else {
            Drawable launchScreenDrawable = this.getLaunchScreenDrawableFromActivityTheme();
            if (launchScreenDrawable == null) {
                return null;
            } else {
                View view = new View(this);
                view.setLayoutParams(matchParent);
                view.setBackground(launchScreenDrawable);
                return view;
            }
        }
    }

    private Drawable getLaunchScreenDrawableFromActivityTheme() {
        TypedValue typedValue = new TypedValue();
        if (!getTheme().resolveAttribute(android.R.attr.windowBackground, typedValue, true)) {
            return null;
        } else if (typedValue.resourceId == 0) {
            return null;
        } else {
            try {
                return getResources().getDrawable(typedValue.resourceId);
            } catch (Resources.NotFoundException e) {
                Log.e(TAG, "Referenced launch screen windowBackground resource does not exist");
                return null;
            }
        }
    }

    private Boolean showSplashScreenUntilFirstFrame() {

        return true;

//        try {
//            ActivityInfo activityInfo = getPackageManager().getActivityInfo(getComponentName(),
//                    PackageManager.GET_META_DATA | PackageManager.GET_ACTIVITIES);
//            Bundle metadata = activityInfo.metaData;
//            return metadata != null && metadata.getBoolean(SPLASH_SCREEN_META_DATA_KEY);
//        } catch (PackageManager.NameNotFoundException e) {
//            return false;
//        }
    }

    private void addLaunchView() {
        if (this.launchView != null) {
            this.addContentView(this.launchView, matchParent);
            this.getWindow().setBackgroundDrawable(null);

//            this.flutterView.addFirstFrameListener(new FirstFrameListener() {
//                public void onFirstFrame() {
//                    MyFlutterActivityDelegate.this.launchView.animate().alpha(0.0F).setListener(new AnimatorListenerAdapter() {
//                        public void onAnimationEnd(Animator animation) {
//                            ((ViewGroup) MyFlutterActivityDelegate.this.launchView.getParent()).removeView(MyFlutterActivityDelegate.this.launchView);
//                            MyFlutterActivityDelegate.this.launchView = null;
//                        }
//                    });
//                    MyFlutterActivityDelegate.this.flutterView.removeFirstFrameListener(this);
//                }
//            });
        }
    }

}
